﻿app.controller("widget-manager", ["$scope", "$timeout", "$modal", "panelService",
    function ($scope, $timeout, $modal, panelService) {
        $scope.targetPanel = null;
        $scope.widgets = [];

        $scope.presetWidgetTypes = [
            {
                rendererId: "TemplatedWidgetRenderer",
                templateId: "line",
                name: "Line (ChartNew)",
                templateData: null
            },
            {
                rendererId: "TemplatedWidgetRenderer",
                templateId: "bar",
                name: "Bar (ChartNew)",
                templateData: null
            },
            {
                rendererId: "TemplatedWidgetRenderer",
                templateId: "pie",
                name: "Pie (ChartNew)",
                templateData: null
            },
            {
                rendererId: "TemplatedWidgetRenderer",
                templateId: "justgage",
                name: "Gauge (Justgage)",
                templateData: null
            },
        ];

        $scope.scratchWidget = {
            rendererId: "",
            markupTemplate: "",
            cssTemplate: "",
            scriptTemplate: "",
            sampleTemplate: "",
            templateData: [],
            usePreset: function (preset) {
                if (preset) {
                    this.markupTemplate = preset.templateId;
                    this.cssTemplate = preset.templateId;
                    this.scriptTemplate = preset.templateId;
                    this.sampleTemplate = preset.templateId;
                    this.rendererId = preset.rendererId;
                    this.templateData = preset.templateData;
                } else {
                    this.markupTemplate = "";
                    this.cssTemplate = "";
                    this.scriptTemplate = "";
                    this.sampleTemplate = "";
                    this.rendererId = "";
                    this.templateData = null;
                }
            }
        };

        $scope.init = function () {
            panelService.list()
                .then(function(httpResponse) {
                    $scope.panels = httpResponse.data;
                });

            $scope.$watch("targetPanel", function (newVal) {
                if (newVal == null)
                    return;

                // load widgets for this panel
                panelService.getWidgets(newVal.id)
                    .then(function(httpResponse) {
                        $scope.widgets = httpResponse.data;
                    });
            });
        };

        $scope.openWidgetAddDialog = function (preset) {
            $scope.scratchWidget.usePreset(preset);

            var modalInstance = $modal.open({
                templateUrl: "/Content/templates/widget-add-template.html",
                controller: "widget-add-dialog",
                size: "lg",
                resolve: {
                    widget: function() {
                        return $scope.scratchWidget;
                    } 
                }
            });

            modalInstance.result.then(function(widget) {
                $scope.scratchWidget = widget;


            });
        };

        $timeout(function() {
            $scope.init();
        }, 5);
    }
]);